//r149

// check data in the Create Account Form
function validateAccountForm()
{
	var acctFormObj = document.getElementById("createAccount");

	var titleVar = document.forms["createAccountForm"]["title"].value;
	var firstNameVar = document.forms["createAccountForm"]["firstname"].value;
	var lastNameVar = document.forms["createAccountForm"]["lastname"].value;

	var genderObj = document.getElementsByName("gender");

	var phoneVar = document.forms["createAccountForm"]["phone"].value;
	//var emailFld = acctFormObj.email;
	var emailVar = document.forms["createAccountForm"]["email"].value;

	var pwdFld = acctFormObj.password;
	var pwdVar = document.forms["createAccountForm"]["password"].value;
	var cfmpwdFld = acctFormObj.confirmPassword;
	var cfmpwdVar = document.forms["createAccountForm"]["confirmPassword"].value;

	var acctTypeObj = document.getElementsByName("acctType");

	var titleOk, firstNameOk, lastNameOk, genderOk, phoneOk, emailOk, psdOk, cfmpsdOk, psdSameOk, typeOk;

	titleOk = validateTitle(titleVar);
	firstNameOk = validateFirstName(firstNameVar);
	lastNameOk = validateLastName(lastNameVar);
	genderOk = validateGender(genderObj);
	//phoneOk = validatePhone(phoneVar);
	//emailOk = validateEmail(emailFld);\
	emailOk = validateEmail(emailVar);
	//pwdOk = validatePsd(pwdFld);
	//cfmpwdOk = validatePsd(cfmpwdFld);
	//pwdSameOk = validatePsdSame(psdVar,cfmpsdVar);
	pwdOk = validatePassword(pwdFld, cfmpwdFld, emailVar);
	typeOk = validateType(acctTypeObj);

	//return titleOk && firstNameOk && lastNameOk && genderOk && phoneOk && emailOk && psdOk && cfmpsdOk && psdSameOk && typeOk;
	return titleOk && firstNameOk && lastNameOk && genderOk && phoneOk && emailOk && psdOk && typeOk;
}

function validateTitle(titleVar)
{
	if (titleVar==null || titleVar=="")
	{
	alert("Error: Title must be filled out!");
	return false;
	}
}

function validateFirstName(nameVar)
{
	if (nameVar==null || nameVar=="")
	  {
	  alert("Error: First name must be filled out!");
	  return false;
	  }
}

function validateLastName(nameVar)
{
	if (nameVar==null || nameVar=="")
	  {
	  alert("Error: Last name must be filled out!");
	  return false;
	  }
}


function validateGender(radios)
{
  	var i=0;
  	var genderValid=false;
  	while (i < radios.length) {
        if (radios[i].checked) genderValid = true;
        i++;
    }
    if (!genderValid) alert("Error: Must select a gender!");
    return genderValid;
}

//http://blog.stevenlevithan.com/archives/validate-phone-number
function validatePhone(phoneVar)
{
	var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

	if (regexObj.test(phoneVar)) {
	    var formattedPhoneNumber =
	        phoneVar.replace(regexObj, "($1) $2-$3");
	} else {
	    alert("Error: Invalid phone number! Correct format:(xxx)xxx-xxxx.");
	}
}

function validateEmail(email)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
	{
	   return true;
	}

	alert("You have entered an invalid email address!");
    return false;
}

//http://webcheatsheet.com/javascript/form_validation.php
function validatePassword(fld)
{
	var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Error: You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "Error: The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "Error: The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "Error: The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validatePsdSame(psdVar,cfmpsdVar)
{
	var psdSame = false;
	if(psdVar != cfmpsdVar){
		alert("Error: The password doesn't match!");
	}
	else{
		psdSame = true;
	}
	return psdSame;
}

function validatePassword(pwd1, pwd2, emailVar)
{
	if(pwd1.value != "" && pwd1.value == pwd2.value) {
		  if(pwd1.value.length < 6) {
			alert("Error: Password must contain at least six characters!");
			pwd1.focus();
			return false;
		  }
		  if(pwd1.value == emailVar) {
			alert("Error: Password must be different from Username (email)!");
			pwd1.focus();
			return false;
		  }
		  re = /[0-9]/;
		  if(!re.test(pwd1.value)) {
			alert("Error: password must contain at least one number (0-9)!");
			pwd1.focus();
			return false;
		  }
		  re = /[a-z]/;
		  if(!re.test(pwd1.value)) {
			alert("Error: password must contain at least one lowercase letter (a-z)!");
			pwd1.focus();
			return false;
		  }
		  re = /[A-Z]/;
		  if(!re.test(pwd1.value)) {
			alert("Error: password must contain at least one uppercase letter (A-Z)!");
			pwd1.focus();
			return false;
		  }
		}
	else {
		  alert("Error: Please check that you've entered and confirmed your password!");
		  pwd1.focus();
		  return false;
		}

	return true;
}



function validateType(radios)
{
  	var i=0;
  	var typeValid=false;
  	while (i < radios.length) {
        if (radios[i].checked) typeValid = true;
        i++;
    }
    if (!typeValid) alert("Error: Must select an account type!");
    return genderypeValid;
}